<?php

namespace App\Http\Controllers;

use Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Storage;

//用户注册、登录

class UserController extends Controller
{
    public function __construct() {
        $this->photo = '/ucenter/images/head.png';
    }
    //注册
    public function userRegister(Request $request){
        if($request->session()->get('eai')){
            return redirect('hd');
        }else {
            return view('user.register');
        }
    }
    
    
    //登录
    public function userLogin(Request $request){
        if($request->session()->get('eai')){
            return redirect('hd');
        }else{
            return view('user.login');
        }
    }
  
     //注册验证
    public function ConfirmRegister(Request $request){
         $this->validate($request, [
                'username' => 'required|alpha_dash|between:2,6',
                'password' => 'required|min:8',
                'password_confirmation' => 'required|same:password',
                'tel' => 'required',
//                'mt4' => 'required',
//                'server_name' => 'required',
            ]);
            $data=[
                'username'=>trim($request->username),
                'password'=>md5(trim($request->password)),
                'tel'=>trim($request->tel),
//                'mt4'=>trim($request->mt4),
                'invite_code'=>trim($request->invitecode),
//                'server_name'=>trim($request->server_name),
                'reg_time'=>time()
            ];
          //发送报文start
          $follower_tel = trim($request->tel);
          $infodata = "{
                      head: {
                        account: '$follower_tel',
                        tradecode: 8804
                      },
                      body: {
                        id: '-1',
                        analyst_tel: 'AAAA',
                        follower_tel: '$follower_tel',
                        status :'1',
                        direct :'0'
                      }
                   }&
                   ";
        $result = HandanController::socketHandle($infodata);
        $result = substr($result, 0 ,  (strlen($result)-1));
        //缓存报文数据
        Storage::append(date('Y-m-d').'regfollowfile.txt', '注册订阅分析师'.date('Y-m-d H:i:s').$infodata."<br/>返回报文结果：".$result); //缓存数据
        $result = json_decode($result,true);
        if($result['ret_code'] == '0000'){ //报文发送成功
            //注册信息入库
            $id = DB::table('waihui_member')->insertGetId($data);
            if ($id) {
                $request->session()->put('eai', array('id' => $id, 'tel' => trim($request->tel), 'username' => trim($request->username), 'photo' => $this->photo));
                echo json_encode(array('status' => 1));
            } else {
                //注册失败
                echo json_encode(array('status' => 0));
            }
        }else {
            //报文发送失败
            echo json_encode(array('status'=>0));
            return;
        }
        //发送报文end
    }
    //登录验证
    public function ConfirmLogin(Request $request){
        $this->validate($request, [
            'tel' => 'required',
            'password' => 'required'
        ]);  
//        $ip = UserController::GetIP();
//        $location = UserController::getCity($ip);
        $time = time();
        $type = intval(trim($request->type)) ? intval(trim($request->type)) : 0;
        //file_put_contents(base_path().'/test.txt', $location);
//        $location = $location['country'].$location['area'].$location['region'].$location['city'].$location['county'];
        $tel      = trim($request->tel);
        $info = DB::table('waihui_member')->where(array('tel'=>$tel))->first();    //验证用户是否存在
        if($info){
            //如果是PIN账号登录的话，判断用户是否已激活
            $pinStatus = DB::table('waihui_account')->where(array('tel'=>$tel))->value('active_time');
            $jihuo = ($pinStatus>0) ? 1 : 0; //1：代表用户已激活,  0:未激活
            
            if(($type == 1 && md5(trim($request->password)) == $info->password) || (md5(trim($request->password)) == $info->password)){  //判断密码是否正确
               if(trim($request->remeber) == 'on'){
                   setcookie('logintel',$tel);
                   setcookie('loginpass',trim($request->password));
               }
               $photo = $info->photo?$info->photo:$this->photo;
               $inputData=array('id'=>$info->id,'tel'=>$info->tel,'username'=>$info->username,'photo'=>$photo);
               if($request->session()->has('eai')){    // 如果以该用户的session存在，那么判断这个session的值是否正确
                    $session_data = $request->session()->get('eai');
                    if($session_data['tel'] == $info->tel && $session_data['username'] == $info->username){
                        //如果登录成功，那么插入记录到waihui_login_record
                        
//                        $id=DB::table('waihui_login_record')->insert(['userid'=>$session_data['id'],'ip'=>"$ip",'location'=>"$location",'logintime'=>"$time"]);
//                        if($id == 1){
                            echo json_encode(array('status'=>1,'jihuo'=>$jihuo));
//                        }else{
//                            echo json_encode(array('status'=>0));
//                        }
                        
                        
                    }else{
                        $request->session()->forget('eai');    //如果此session的值错误，删除并且重建
                        $request->session()->put('eai',$inputData);
                        echo json_encode(array('status'=>0));
                    }
                }else{  //如果该用户的session不存在，那么创建这个session
                    $request->session()->put("eai",$inputData);
//                    $id=DB::table('waihui_login_record')->insert(['userid'=>$info->id,'ip'=>"$ip",'location'=>"$location",'logintime'=>"$time"]);
//                    if($id == 1){
                        echo json_encode(array('status'=>1,'jihuo'=>$jihuo));
//                    }else{
//                        echo json_encode(array('status'=>0));
//                    }
                }
            }else{
                echo json_encode(array('status'=>0));
            }
        }else{
            echo json_encode(array('status'=>0));
        }
    }
    
    public function GetIP() {   //获取用户的ip地址
        if (!empty($_SERVER["HTTP_CLIENT_IP"])) {  
            $cip = $_SERVER["HTTP_CLIENT_IP"];  
        } else if (!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {  
            $cip = $_SERVER["HTTP_X_FORWARDED_FOR"];  
        } else if (!empty($_SERVER["REMOTE_ADDR"])) {  
            $cip = $_SERVER["REMOTE_ADDR"];  
        } else {  
            $cip = '';  
        }  
        preg_match("/[\d\.]{7,15}/", $cip, $cips);  
        $cip = isset($cips[0]) ? $cips[0] : 'unknown';  
        unset($cips);  
        return $cip;  
    }    
    
    public function getCity($ip){ //获取地区
        if($ip == ''){
            $url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json";//新浪借口获取访问者地区
            $ip=json_decode(file_get_contents($url),true);
            $data = $ip;
        }else{
            $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;//淘宝接口需要填写ip
            $ip=json_decode(file_get_contents($url));  
            if((string)$ip->code=='1'){
               return false;
            }
            $data = (array)$ip->data;
        }
        return $data;  
    }  
    
    
    //找回密码
    public function forgetPswd(){
        if(isMobile()) {
        	return view('mobile.forget');
        }else {
        	return view('user.getpassword');
        }
        
    }
    
    
    //找回密码操作
    public function ConfirmForgetpswd(Request $request){
        $this->validate($request, [
            'tel' => 'required',
            'password' => 'required|min:8',
            'password_confirmation' => 'required|same:password',
        ]);
        $tel = trim($request->tel);
        $id = DB::table('waihui_member')->where(array('tel'=>$tel))->value('id');
        $password = md5(trim($request->password));
        if($id){
            DB::table('waihui_member')->where(array('id'=>$id))->update(['password'=>""]);
            $result = DB::table('waihui_member')->where(array('id'=>$id))->update(['password'=>"$password"]);
            if($result){
                echo json_encode(array('status'=>0)); //修改成功
            }else{
                echo json_encode(array('status'=>1)); //修改失败
            }
        }else{
            //用户不存在
            echo json_encode(array('status'=>2));
        }
    }
    
    //账户安全 修改密码
    public function updatepwd(Request $request){
        $tel = $request->session()->get('eai')['tel'];
        $this->validate($request, [
            'j_user_password' => 'required|string',
            'newPassWord' => 'required|min:6|max:10',
        ]);        
        $info = DB::table('waihui_member')->select('id','username','password','tel','photo')->where(array('tel'=>$tel))->first();
        //dd($info);
        $password = md5(trim($request->j_user_password));
        $newpassword = md5(trim($request->newPassWord));
        if($info){
            if($password != $info->password){  //原密码错误
                $res['status'] =2 ;
                echo json_encode($res);
            }else{
                DB::table('waihui_member')->where(array('id'=>$info->id))->update(['password'=>""]);
                $result = DB::table('waihui_member')->where(array('id'=>$info->id))->update(['password'=>"$newpassword"]);
                $inputData=array('id'=>$info->id,'tel'=>$info->tel,'username'=>$info->username,'photo'=>$info->photo);
                $request->session()->forget('eai');    //如果此session的值错误，删除并且重建
                $request->session()->put('eai',$inputData);
                if($result){
                    $res['status'] =1 ;
                    echo json_encode($res);
                   
                }else{
                    $res['status'] =0 ;
                    echo json_encode($res);
                   
                }
            }
        }else{
            //用户不存在
            $res['status'] =0 ;
            echo json_encode($res);
            
        }
        
        
      
            
        
    }
    
    //账户安全 修改密码 根据手机号修改验证码
    public function updatetelpwd(Request $request){
        $tel  = $request->session()->get('eai')['tel'];
        $info = DB::table('waihui_member')->select('id','username','password','tel','photo')->where(array('tel'=>$tel))->first();
        $newpassword = md5(trim($request->newPassWord));
        if($info){
            $result = DB::table('waihui_member')->where(array('id'=>$info->id))->update(['password'=>"$newpassword"]);
            $inputData=array('id'=>$info->id,'tel'=>$info->tel,'username'=>$info->username,'photo'=>$info->photo);
            $request->session()->forget('eai');    //如果此session的值错误，删除并且重建
            $request->session()->put('eai',$inputData);
            if($result){
                $res['status'] =1 ;
                echo json_encode($res);
               
            }else{
                $res['status'] =0 ;
                echo json_encode($res);
            }
        }else{
            //用户不存在
            $res['status'] =0 ;
            echo json_encode($res);
        }

    }
    
    //退出登录
    public function loginout(Request $request){
        $request->session()->flush();
        setcookie("logintel", "", time()-3600);
        setcookie("loginpass", "", time()-3600);
        if(isMobile()){
            return redirect('mlogin');
        }else{
            return redirect('login');
        }
    }
    //订阅/取消订阅分析师
    public function fllow(Request $request){
        $sessiondata = $request->session()->get('eai');
        if(!$sessiondata){
            echo json_encode(array('status'=>2,'msg'=>'用户未登录'));
            return;
        }
        $isvip = DB::table('waihui_member')->where(array('id'=>$sessiondata['id']))->value('isvip'); //查看该用户是不是vip
        if($isvip == '0'){
            echo json_encode(array('status'=>4,'msg'=>'请先完成充值'));
            return;
        }
        if($sessiondata)
        $limitCount  = 3; //订阅分析师限制数量
        $data['follower_id']     = trim($sessiondata['id']);
        $data['follow_name']     = trim($sessiondata['username']);
        $data['analyst_id']      = trim($request->analystid);
        $data['analyst_name']    = trim($request->analystname);
        $data['follow_time']     = time();
        $data['status']          = trim($request->status);//1：订阅  0：取消订阅
        $data['direction']       = trim($request->direction); //0：正向 1：反向
        $counts      = DB::table('waihui_follow')->where([['follower_id','=',$sessiondata['id']],['status','=',1]])->count();
        $isexist     = DB::table('waihui_follow')->where([['follower_id','=',$sessiondata['id']],['analyst_id','=',$data['analyst_id']]])->first();
        if((!$isexist || ($isexist->status == 0)) && ($data['status'] == '1') && ($counts >= $limitCount)){ //订阅分析师不能操作$limitCount个
            echo json_encode(array('status'=>3,'msg'=>'订阅分析师数量不能超过'.$limitCount.'个'));
            return;
        }
        $direct       = intval($data["direction"]);
        $status       = intval($data['status']);
        $analyst_tel  = DB::table('waihui_member')->where(array('id'=>$data['analyst_id']))->value('tel');
        $follower_tel = DB::table('waihui_member')->where(array('id'=>$data['follower_id']))->value('tel');
        if(!$isexist){
            $id = DB::table('waihui_follow')->insertGetId($data);
            //订阅分析师
            $infodata="{   
              head: {
                account: '$follower_tel',
                tradecode: 8804
              },
              body: {
                id: '$id',
                analyst_tel: '$analyst_tel',
                follower_tel: '$follower_tel',
                status :'$status',
                direct :'$direct' 
              }
           }&
           ";

            $result = HandanController::socketHandle($infodata);
            $result = substr($result, 0 ,  (strlen($result)-1));
            //缓存报文数据
            Storage::append(date('Y-m-d').'followfile.txt', '订阅分析师'.date('Y-m-d H:i:s').$infodata."<br/>返回报文结果：".$result); //缓存数据
            $result = json_decode($result,true);
            if($result['ret_code'] == '0000'){ //报文发送成功
                //订阅/取消订阅 操作成功
                echo json_encode(array('status'=>1,'msg'=>'操作成功'));
                return;
            }else{//报文发送失败
                DB::table('waihui_follow')->where(array('id'=>$id))->delete();
                //订阅/取消订阅 操作失败
                echo json_encode(array('status'=>0,'msg'=>'操作失败'));
                return;
            }
        }else{
            /*取消订阅时 强制跟单平仓start*/
            if($status == 0){
                $unclosearr = DB::table('waihui_gendan')->where([['analyst_tel','=',"$analyst_tel"],['follower_tel','=',"$follower_tel"],['isstop','=',1]])->select('tradeid')->get();
                if($unclosearr){
                     $tradeid = '';
                     foreach ($unclosearr as $uk=>$uv){
                        $tradeid .= $uv-> tradeid.'|';
                     }  
                    $infodata="{   
                        head: {
                          account: '$analyst_tel',
                          tradecode: 8803,
                          isCancelSubscript:1,
                          allsend: '$follower_tel'
                        },
                        body: {
                            fromsource: 'netpage',
                            trader_orderid : '$tradeid',
                        }
                    }&
                    ";
                    $result = HandanController::socketHandle($infodata);
                    $result = substr($result, 0 ,  (strlen($result)-1));
                    //缓存报文数据
                    Storage::append(date('Y-m-d').'followclose.txt', '取消订阅时强制跟单平仓'.date('Y-m-d H:i:s').$infodata."<br/>返回报文结果：".$result); //缓存数据
                    $result = json_decode($result,true);
                    if($result['ret_code'] == '0000'){
                        /*取消订阅时 强制跟单平仓end*/

                        $id = $isexist->id;
                        //订阅分析师
                        $infodata="{   
                          head: {
                            account: '$follower_tel',
                            tradecode: 8804
                          },
                          body: {
                            id: '$id',
                            analyst_tel: '$analyst_tel',
                            follower_tel: '$follower_tel',
                            status :'$status',
                            direct :'$direct' 
                          }
                       }&
                       ";
                        $result_dingyue = HandanController::socketHandle($infodata);
                        $result_dingyue = substr($result_dingyue, 0 ,  (strlen($result_dingyue)-1));
                        //缓存报文数据
                        Storage::append(date('Y-m-d').'followfile.txt', '编辑订阅分析师'.date('Y-m-d H:i:s').$infodata."<br/>返回报文结果：".$result_dingyue); //缓存数据
                        $result = json_decode($result_dingyue,true);
                    }else{ //平仓报文没有发送成功
                        echo json_encode(array('status'=>0,'msg'=>'操作失败'));
                        return;
                    }
                    
                }else{  //如果没有需要平仓的订单，只发送编辑订阅方向的报文
                    $id = $isexist->id;
                    //订阅分析师
                    $infodata="{   
                      head: {
                        account: '$follower_tel',
                        tradecode: 8804
                      },
                      body: {
                        id: '$id',
                        analyst_tel: '$analyst_tel',
                        follower_tel: '$follower_tel',
                        status :'$status',
                        direct :'$direct' 
                      }
                    }&
                    ";
                    $result_dingyue = HandanController::socketHandle($infodata);
                    $result_dingyue = substr($result_dingyue, 0 ,  (strlen($result_dingyue)-1));
                    //缓存报文数据
                    Storage::append(date('Y-m-d').'followfile.txt', '编辑订阅分析师'.date('Y-m-d H:i:s').$infodata."<br/>返回报文结果：".$result_dingyue); //缓存数据
                    $result = json_decode($result_dingyue,true);
                }
                $data['direction'] = $isexist->direction; //取消订阅 保存原有方向
            }else{
                /*取消订阅时 强制跟单平仓end*/

                $id = $isexist->id;
                //订阅分析师
                $infodata="{   
                  head: {
                    account: '$follower_tel',
                    tradecode: 8804
                  },
                  body: {
                    id: '$id',
                    analyst_tel: '$analyst_tel',
                    follower_tel: '$follower_tel',
                    status :'$status',
                    direct :'$direct' 
                  }
               }&
               ";
                $result = HandanController::socketHandle($infodata);
                $result = substr($result, 0 ,  (strlen($result)-1));
                //缓存报文数据
                Storage::append(date('Y-m-d').'followfile.txt', '编辑订阅分析师'.date('Y-m-d H:i:s').$infodata."<br/>返回报文结果：".$result); //缓存数据

                $result = json_decode($result,true);
            }
            
            if($result['ret_code'] == '0000'){ //报文发送成功
                $id = DB::table('waihui_follow')->where([['follower_id','=',$sessiondata['id']],['analyst_id','=',$data['analyst_id']]])->update(['status'=>$data['status'],'direction'=>$data['direction'],'update_time'=>time()]);
                if($id){
                    //订阅/取消订阅 操作成功
                    echo json_encode(array('status'=>1,'msg'=>'操作成功'));
                    return;
                }else{
                    //订阅/取消订阅 操作失败
                    echo json_encode(array('status'=>0,'msg'=>'操作失败'));
                    return;
                }
            }else{//报文发送失败
                //订阅/取消订阅 操作失败
                echo json_encode(array('status'=>0,'msg'=>'操作失败'));
                return;
            }
        }
    }
    
    
    
    //验证用户名是否存在
    public function checkExistUsername(Request $request){
        $username = trim($request->username);
        $type     = $request->type; //  1 用于用户修改资料判断
        $type     = $type?$type:0;
        $info     = DB::table('waihui_member')->where('username',$username)->first();
        if($info){
            if($type == 1){
                $session = $request->session()->get('eai');
                if($session['id'] == $info->id){
                    return 0; //用户名不存在
                }else{
                    return 1; //用户名存在
                }
            }else{
                return 1; //用户名存在
            }
        }else{
            return 0; //用户名不存在
        }
    }
    //验证手机号是否存在
    public function checkExistPhone(Request $request){
        $tel      = trim($request->tel);
        $info     = DB::table('waihui_member')->where('tel',$tel)->first();
        if($info){
            return 1; //手机号存在
        }else{
            return 0; //手机号不存在
        }
    }
    
    
    public  function myCenter(Request $request){   //用户中心
        $session_data = $request->session()->get('eai');
        if(!$session_data){
            return view('user.login');
        }
        
        $id = $session_data['id'];

        //判断有没有该分析师
        $userinfo = DB::table('waihui_member')->select('*')->where('id','=',$id)->first();
        if(!$userinfo){
            return view('user.login');
        }else{
           $moreinfo = DB::table('waihui_member as m')
                    ->join('waihui_member_info as i','m.id','=','i.userid')                   
                    ->select('i.province','i.city','i.country')
                    ->where('m.id','=',$id)
                    ->first();
        }
        $userinfo->reg_time = date('Y-m-d H:i:s',$userinfo->reg_time);
        $follownum    = DB::table('waihui_follow')->where([['analyst_id','=',$id],['status','=',1]])->count();
        //订阅者信息
        $fwhere['f.analyst_id'] = $id;
        $fwhere['f.status']     = 1; //已订阅
        $followdata   = DB::table('waihui_follow as f')
                        ->join('waihui_member as m','f.follower_id','=','m.id')
                        ->select('m.photo','m.username')
                        ->where($fwhere)
                        ->limit(6)->get()->toArray();
        //dd($userinfo);
        if($userinfo->isanalyst == 1){  //分析师
            return view('user.analyhome',compact('userinfo','moreinfo','follownum','followdata'));
        }else if($userinfo->isanalyst == 0){   //普通客户
            return view('user.myhome',compact('userinfo','moreinfo','follownum','followdata'));
        }
    }
    
    //账号生成
    public function accountNum(Request $request){
        $str    = trim($request->pre); //中间两个字母
        $start  = intval(trim($request->start));//截取本次入库数组的开始位置
        $end    = intval(trim($request->end));//截取本次入库的条数
        $code   = trim($request->code);//邀请码
        $start  = $start?$start:0;
        $end    = $end?$end:10;
        $code   = $code?$code:'000000';
        $data   = $this->getAccount($str,$code); //获取账号
        $data   = array_slice($data,$start,$end);//获取本次入库的账号
        $i = 0;
        foreach ($data as $k=>$v){
            //检查账号是否存在  防止信息重复
            $isexist = DB::table('waihui_member')->where([['tel','=',$v['tel']]])->first();
            if(!$isexist){
                $pin = $v['pin'];
                unset($v['pin']);//member表没有PIN字段 将其字段删除
                $id  = DB::table('waihui_member')->insertGetId($v);
                if($id){
                    DB::table('waihui_account')->insert(['tel'=>$v['tel'],'pin'=>$pin]);
                    $i++;
                }
            }
        }
        echo '共插入'.$i.'记录';
    }
    //账号生成方法
    public function  getAccount($str,$code){
        //        $zimu = array('a','b');//,'c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
        $num  = array(1,2,3,4);//,5,6,7,8,9
        $arrdata = [];
//        foreach ($zimu as $k1=>$v1){
//            foreach ($zimu as $k2=>$v2){
                foreach ($num as $k3=>$v3){
                    foreach ($num as $k4=>$v4){
                        foreach ($num as $k5=>$v5){
                            foreach ($num as $k6=>$v6) {
                                $tel = "EAI$str$v3$v4$v5$v6";
                                $data['username']    = $tel;
                                $data['tel']         = $tel;
                                $data['password']    = md5(12345678);
                                $data['pin']         = rand(0,9).rand(0,9)."$v5$v3$v6$v4";//防止猜出别人的账号信息，前两位随机取值，后四位顺序打乱
                                $data['star']        = '0';
                                $data['grade']       = '-1';
                                $data['authstatus']  = '0';
                                $data['photo']        = "http://www.eaifx.com/ucenter/images/defaultheadimg/default_6.jpg";
                                $data['invite_code']  = "$code";
//                                array_push($arrdata,array($tel,$data['pin']));
                                array_push($arrdata,$data);
                            }
                        }
                    }
                }
//            }
//        }
        return $arrdata;
    }
    
    //查询用户漏单记录
    public function louDan(Request $request){
        if($request->get('id')){
            $id = intval(trim($request->get('id')));
            $tel = DB::table('waihui_member')->where('id',$id)->value('tel');
        }else if($request->get('username')){
            $username = htmlspecialchars(trim($request->get('username')));
            $member = DB::table('waihui_member')->select('id','tel')->where('username',$username)->first();
            $id  = $member->id;
            $tel = $member->tel;
        }else if($request->get('tel')){
            $tel = trim($request->get('tel'));
            $id = DB::table('waihui_member')->where('tel',$tel)->value('id');
        }
        
        if(!$tel || !$id){
            echo '没有此用户';
            return false;
        }else{
            echo "该用户手机号:$tel<br/>";
        }
        $follow = DB::table('waihui_follow as f')
                ->join('waihui_member as m','f.analyst_id','m.id')
                ->select('m.tel','m.username','f.follow_time','f.update_time','f.direction')->where([['follower_id','=',$id],['status','=',1]])->get();
        if(!$follow){
            echo '该用户没有订阅分析师';
            return false;
        }
        foreach($follow as $k=>$v){
            $followTime = $v->update_time != '' ? $v->update_time : $v->follow_time;
            echo "该用户订阅了: $v->username ，订阅时间: ".date('Y-m-d H:i:s',$followTime)."，方向：$v->direction, 手机号：$v->tel<br/>";
            $follow[$k]->handan = DB::table('waihui_order')->select('tradeid')->where([['tel','=',$v->tel],['begin_time','>',$followTime]])->orderBy('begin_time','desc')->get();
            $follow[$k]->gendan  = DB::table('waihui_gendan')->select('tradeid')->where([['follower_tel','=',$tel],['analyst_tel','=',$v->tel],['begin_time','>',$followTime]])->orderBy('begin_time','desc')->get();
            $allhandancount = count($follow[$k]->handan);
            $allgendancount = count($follow[$k]->gendan);
            echo $v->username."喊单记录,共<font color=red>".$allhandancount."</font>单：<br/>";
            $handan = array();
            if($follow[$k]->handan){
                foreach($follow[$k]->handan as $kk=>$kv){
                    echo $kv->tradeid.',';
                    $handan[] = trim($kv->tradeid);
                }
            }
            
            echo "<br/>";
            
            echo "该客户跟随".$v->username."跟单记录：共<font color=red>".$allgendancount."</font>单：<br/>";
            $gendan = array();
            if($follow[$k]->gendan){
                foreach($follow[$k]->gendan as $f){
                    echo $f->tradeid.',';
                    $gendan[] = trim($f->tradeid);
                }
            }   
            
            echo "<br/>";
            echo "<font color=red>漏单：</font><br/>";
            foreach($handan as $vv){
                if(!in_array($vv, $gendan)){
                    $orderData = DB::table('waihui_order')->select('begin_time','typename')->where('tradeid',$vv)->first();
                    echo $vv.'(<font color=red>入场时间'.date('Y-m-d H:i:s',$orderData->begin_time).',品种：'.$orderData->typename.'</font>),漏单原因：';
                    $reason = DB::table('waihui_gendan_fail')->where([['tradeid','=',$vv],['follower_tel','=',$tel]])->value('reason');
                    if($reason){
                        echo $reason."<br/>";
                    }else{
                        echo '跟单失败表里没有记录这条信息<br/>'; 
                    }
                }
            }
            echo "<br/>";
        } 
        
        
//        dd($handan,$gendan);
    }
    
    //开通试用账号，默认七天到期
    public function vip(Request $request){
        $tel  = trim($request->get('tel'));
        $data = DB::table('waihui_member')->select('isvip','expire_time')->where('tel',$tel)->first();
        if(!$data){
            echo '没有此用户';
            return false;
        }
        if($data->expire_time > time()){
            echo '您的体验账户还没有过期';
            return false;
        }else{
            $update['expire_time'] = strtotime('+7 days');
            $update['isvip'] = '3';
            $res = DB::table('waihui_member')->where('tel',$tel)->update($update);
            if($res){
                echo '已经开通体验';
                return false;
            }else{
                echo '操作失败';
            }
        }
    }             
}
